package cn.piaoyi.comm;

import java.io.File;
import java.util.Date;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FilenameUtils;

import cn.piaoyi.bean.Daily;
import cn.piaoyi.web.servlet.UserSession;


public class PiaoyiUtil {
	public static UserSession getUser(HttpServletRequest request){
		UserSession us = (UserSession) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
		if (us == null) {
			us = new UserSession();
			request.getSession().setAttribute(Constant.USER_SESSION_KEY,us);
		}
		return us;
	}
	
	public static String mkDirByNow(String picPath){
		String path = picPath + "/" + Util.formatDate(new Date(),"yyyyMMdd");
		File f = new File(Constant.ROOTPATH + "/" + path);
		if(!f.exists()){
			f.mkdirs();
		}
		return path;
	}
	public static String mkFCKDirByNow(String picPath){
		String nowDir = Util.formatDate(new Date(),"yyyyMMdd");
		String path = picPath + "/" + nowDir;
		File f = new File(path);
		if(!f.exists()){
			f.mkdirs();
		}
		return nowDir;
	}
	
	public static boolean isAllowPicFile(String fileName) {
		String[] filExts = { "jpg", "jpeg", "gif" };
		return FilenameUtils.isExtension(fileName, filExts);
	}
	
	public static String formatWeek(int week){
		switch(week){
		case 1: return "星期一";
		case 2: return "星期二";
		case 3: return "星期三";
		case 4: return "星期四";
		case 5: return "星期五";
		case 6: return "星期六";
		case 0: return "星期日";
		default : return "未知";
		}
	}
	
	public static String getCity(int cityId){
		//西安 北京 上海 广州 深圳 武汉 天津 杭州 重庆 成都 青岛 苏州 郑州 无锡 宁波 南京 大连 沈阳 合肥 温州 长沙 哈尔滨 南宁 济南 太原 长春 石家庄  锦州 
		switch(cityId){
			case 1:return "西安";
			case 2:return "北京";
			case 3:return "上海";
			case 4:return "广州";
			case 5:return "深圳";
			case 6:return "武汉";
			case 7:return "天津";
			case 8:return "杭州";
			case 9:return "重庆";
			case 10:return "成都";
			case 11:return "青岛";
			case 12:return "苏州";
			case 13:return "郑州";
			case 14:return "无锡";
			case 15:return "宁波";
			case 16:return "南京";
			case 17:return "大连";
			case 18:return "沈阳";
			case 19:return "合肥";
			case 20:return "温州";
			case 21:return "长沙";
			case 22:return "哈尔滨";
			case 23:return "南宁";
			case 24:return "济南";
			case 25:return "太原";
			case 26:return "长春";
			case 27:return "石家庄";
			case 28:return "锦州";
			default:return "西安";
		}
	}
	
	public static String formatDailyTime(Daily daily){
		StringBuffer sb = new StringBuffer();
		sb.append(Util.formatDate7(daily.getStartTime()));
		sb.append("(" + PiaoyiUtil.formatWeek(daily.getStartWeek()) + ")");
		
		sb.append(" " + String.format("%02d", daily.getStartHour()));
		sb.append(":" + String.format("%02d", daily.getStartMinute()));
		return sb.toString();
	}
	
	public static String getUUID(){
		String s = UUID.randomUUID().toString(); 
        //去掉“-”符号 
        return s.substring(0,8)+s.substring(9,13)+s.substring(14,18)+s.substring(19,23)+s.substring(24); 
	}
}
